<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

function pagination($obj){
    if (!$obj) return '';

    $params=request()->param();
    return '<div class="imooc-app">'.$obj->appends($params)->render().'</div>';
}

/**
 * 获取栏目名称
 * @param $catId
 */
function getCatName($catId){
    if (!$catId) return '';

    $cat=config('cat.lists');
    return !empty($cat[$catId]) ? $cat[$catId] : '';
}

/**
 * 获取区域名称
 * @param $address
 * @return string
 */
function getAddressName($address){
    if (!$address) return '';

    $addreses=config('address.addresslists');
    return !empty($addreses[$address]) ? $addreses[$address] : '';
}

/**
 * 是否推荐
 * @param $str
 */
function isYesNo($str){
    return $str ? '<span class="label label-success radius">已推荐</span>' : '<span style="background-color: red;" class="label radius">未推荐</span>';
}

/**
 * 状态
 * @param $id
 * @param $status
 */
function status($id,$status){
    $controller=request()->controller();
    $sta = $status == 1 ? 0 : 1;
    $url = url($controller.'/status', ['id' => $id, 'status' => $sta]);

    if($status == 1) {
        $str = "<a href='javascript:;' title='修改状态' status_url='".$url."' onclick='app_status(this)'>
                    <span class='label label-success radius'>正常</span></a>";
    }elseif($status == 0) {
        $str = "<a href='javascript:;' title='修改状态' status_url='".$url."' onclick='app_status(this)'>
                <span class='label label-danger radius'>待审</span></a>";
    }

    return $str;

}

/**
 * 认证
 * @param $id
 * @param $status
 */
function approve($id,$approve){
    $controller=request()->controller();
    $sta = $approve == 1 ? 0 : 1;
    $url = url($controller.'/approve', ['id' => $id, 'is_approve' => $sta]);

    if($approve == 1) {
        $str = "<a href='javascript:;' title='修改状态' status_url='".$url."' onclick='app_status(this)'>
                    <span class='label label-success radius'>正常</span></a>";
    }elseif($approve == 0) {
        $str = "<a href='javascript:;' title='修改状态' status_url='".$url."' onclick='app_status(this)'>
                <span class='label label-danger radius'>待审</span></a>";
    }

    return $str;

}

/**
 * 通用化API接口数据输出
 * @param int $status 业务状态码
 * @param string $message 信息提示
 * @param array $data 数据
 * @param int $httpCode  http状态码
 * @return \think\response\Json
 */
function show($status, $message, $data=[], $httpCode=200){
    $data = [
        'status' => $status,
        'message' => $message,
        'data' => $data,
    ];
    return json($data, $httpCode);
}